from weibosender.db.sql_helper import SqlHelper


class ReceiversDAL:
    _sql_query_uid_count = 'select count(0) as count from receivers where uid=(?);'  # 查询用户是否存在
    _sql_append = 'insert into receivers(uid, screen_name, gender, tag_id, tag_name) values(?,?,?,?,?);'   # 添加用户
    _sql_get_receivers = 'select a.uid, a.screen_name from receivers a ' \
                           'left join sent_msg_log b ' \
                           'on a.uid = b.receiver_id ' \
                           'where b.id is null;'    # 查询未发送消息的用户

    @staticmethod
    def append(user):
        if not user:
            return None
        query_uid_count_result = SqlHelper.fetchone(ReceiversDAL._sql_query_uid_count, (user[0],))
        if query_uid_count_result and query_uid_count_result[0] == 0:
            SqlHelper.execute(ReceiversDAL._sql_append, user)

    '''
    获取指定用户未发送消息的用户
    '''
    @staticmethod
    def get_receivers():
        return SqlHelper.fetchall(ReceiversDAL._sql_get_receivers)